package com.cennavi.minemap.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.minemap.bean.DataBean;
import com.cennavi.minemap.bean.GroupSourceLayerBean;
import com.cennavi.minemap.bean.MessageBean;
import com.cennavi.minemap.bean.SourceBean;
import com.cennavi.minemap.bean.SourceGroupBean;
import com.cennavi.minemap.bean.UserSourceBean;
import com.cennavi.minemap.service.SourceService;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@ComponentScan(basePackages = { "com.cennavi.minemap.service" })
@RequestMapping("/service/source")
public class SourceController {

	ObjectMapper objectMapper = new ObjectMapper();

	@Autowired
	private SourceService sourceService;

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody MessageBean update(@RequestBody String json) throws Exception {
		SourceBean sourceBean = objectMapper.readValue(json, SourceBean.class);
		MessageBean messageBean = new MessageBean();
		int status = sourceService.update(sourceBean);
		if (status < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("source update error");
		} else {
			messageBean.setErrcode(0);
		}
		return messageBean;
	}

	@RequestMapping(value = "/{id}")
	public @ResponseBody MessageBean queryById(@PathVariable int id) {
		MessageBean messageBean = new MessageBean();
		List<SourceBean> list = sourceService.queryById(id);
		if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("source query error");
		} else {
			DataBean<SourceBean> dataBean = new DataBean<SourceBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;

	}

	@RequestMapping(value = "/statistic/{id}")
	public @ResponseBody MessageBean queryStatisticById(@PathVariable int id) {
		MessageBean messageBean = new MessageBean();
		List<JSONObject> statisticData = sourceService.queryStatisticById(id);
		if (statisticData == null) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("datasource no statistic");
		} else {
			DataBean<JSONObject> dataBean = new DataBean<JSONObject>();
			messageBean.setErrcode(0);
			dataBean.setTotal(statisticData.size());
			dataBean.setRows(statisticData);
			messageBean.setData(dataBean);
		}
		return messageBean;

	}

	@RequestMapping(value = "/list")
	public @ResponseBody MessageBean queryById(@RequestParam("userId") int userId,
			@RequestParam("groupId") int groupId) {
		MessageBean messageBean = new MessageBean();
		List<SourceBean> list = sourceService.queryByUserGroupId(userId, groupId);
		if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("source query by userId, groupId error");
		} else {
			DataBean<SourceBean> dataBean = new DataBean<SourceBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;

	}

	@RequestMapping(value = "/tileJson/{id}")
	public @ResponseBody String queryStyle(@PathVariable int id) throws Exception {
		return sourceService.queryTileJsonById(id);
	}

	@RequestMapping(value = "/self/list/{userId}")
	public @ResponseBody MessageBean querySelfById(@PathVariable int userId) {
		MessageBean messageBean = new MessageBean();
		List<SourceBean> list = sourceService.querySelfByUserGroupId(userId);
		if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("source query by userId, groupId error");
		} else {
			DataBean<SourceBean> dataBean = new DataBean<SourceBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;

	}

	@RequestMapping(value = "/layerDesc/{id}")
	public @ResponseBody MessageBean querySourceLayerDescById(@PathVariable int id) {
		MessageBean messageBean = new MessageBean();
		String sourceLayer = sourceService.querySourceLayerDescById(id);
		if (sourceLayer == null) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("source layer query error");
		} else {
			DataBean<JSONArray> dataBean = new DataBean<JSONArray>();
			messageBean.setErrcode(0);
			dataBean.setTotal(1);
			dataBean.setRows(Arrays.asList(JSONArray.parseArray(sourceLayer)));
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/buy")
	public MessageBean sourceBuy(@RequestBody String json) throws Exception {
		UserSourceBean userSourceBean = objectMapper.readValue(json, UserSourceBean.class);
		MessageBean messageBean = new MessageBean();
		int status = sourceService.sourceBuy(userSourceBean);
		if (status < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("source buy error");
		} else {
			messageBean.setErrcode(0);
		}
		return messageBean;
	}

	@RequestMapping(value = "/group/list")
	public @ResponseBody MessageBean queryGroup() {
		MessageBean messageBean = new MessageBean();
		List<SourceGroupBean> list = sourceService.queryGroup();
		if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("source group query error");
		} else {
			DataBean<SourceGroupBean> dataBean = new DataBean<SourceGroupBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/group/list/{userId}")
	public @ResponseBody MessageBean queryGroupByUserId(@PathVariable int userId) {
		MessageBean messageBean = new MessageBean();
		List<SourceGroupBean> list = sourceService.queryGroupByUserId(userId);
		if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("source group query by userId error");
		} else {
			DataBean<SourceGroupBean> dataBean = new DataBean<SourceGroupBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/info/list/{userId}")
	public @ResponseBody MessageBean queryGroupSourceLayerBy(@PathVariable int userId) {
		MessageBean messageBean = new MessageBean();
		List<GroupSourceLayerBean> list = sourceService.queryGroupSourceLayerBy(userId);
		if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("source group all query by userId error");
		} else {
			DataBean<GroupSourceLayerBean> dataBean = new DataBean<GroupSourceLayerBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}
}
